package demo1;

/**
 * @Author liangzai
 * @Description:
 */
public class Training {
    //最长乘积等价子数组
    public int maxLength(int[] nums) {
        int max = 1;
        int n = nums.length;
        for (int i = 0; i < n; i++) {
            int mul = nums[i];
            int gcd = nums[i];
            int lcm = nums[i];
            for(int j = i+1;j< nums.length;j++){
                mul *= nums[j];
                gcd = gcd(gcd, nums[j]);
                lcm = lcm(lcm, nums[j]);
                if (mul == gcd * lcm) {
                    max = Math.max(max,j-i+1);
                }else {
                    break;
                }
            }
        }
        return max;
    }

    private int gcd(int a,int b){
        //return a!=0?gcd(a,b%a):b;
        return b != 0 ? gcd(b, a % b) : a;
    }

    private int lcm(int a,int b){
        return (a*b)/gcd(a,b);
    }
}
